package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.activity.data.YueFanAdapter;
import com.xgkj.diyiketang.data.HuoDongInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.MyHuoDongListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/4/13.
 */
public class MyHuoDongActivity extends BaseActivity implements View.OnClickListener, PullToRefreshListView.OnRefreshListener, PullToRefreshListView.OnLoadDataListener {
    private Context mContext;
    private TextView back;
    private Button mCreate;

    private PullToRefreshListView fqdListView;
    private PullToRefreshListView cydListView;
    private int currentPage_fqd = 1;
    private int currentPage_cyd = 1;
    private int size = 10;
    private YueFanAdapter fqdAdapter;
    private YueFanAdapter cydAdapter;
    private int fqdTaskId = -1;
    private int cydTaskId = -1;
    private CheckBox mFaQiDe;
    private CheckBox mCanYuDe;
    private NoDataUtil noDataUtil;
    private RelativeLayout emptyLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myhuodong);
        mContext = this;
        initView();
    }

    private void initView() {
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(this);
        mCreate = (Button) findViewById(R.id.createhd_btn);
        mCreate.setOnClickListener(this);
        boolean isCan;
        if (0 != getEatShowApp().getmUserInfo().isVip()) {
            isCan = true;
        } else {
            isCan = false;
        }
        if (!isCan) {
            mCreate.setVisibility(View.GONE);
        }

        mFaQiDe = (CheckBox) findViewById(R.id.faqide_btn);
        mFaQiDe.setOnClickListener(this);
        mFaQiDe.setChecked(true);

        mCanYuDe = (CheckBox) findViewById(R.id.canyude_btn);
        mCanYuDe.setOnClickListener(this);
        mCanYuDe.setChecked(false);

        fqdListView = (PullToRefreshListView) findViewById(R.id.my_faqi_huodong);
        cydListView = (PullToRefreshListView) findViewById(R.id.my_canyu_huodong);
        fqdListView.setVisibility(View.VISIBLE);
        cydListView.setVisibility(View.GONE);

        fqdListView.init(mContext, true);
        fqdAdapter = new YueFanAdapter(mContext);
        fqdListView.setAdapter(fqdAdapter, true);
        fqdListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (fqdListView.getHeaderViewsCount() > 0) {
                    position--;
                }
                if (position < 0) return;
                HuoDongInfo fdinfo = (HuoDongInfo) fqdAdapter.getItem(position);
                String activeid = fdinfo.getActivityId();
                String icon = fdinfo.getUserLogo();
                Intent bdInent = new Intent(mContext, YuFanDetailActivity.class);
                bdInent.putExtra("ACTIVEID", activeid);
                bdInent.putExtra("LOGO", icon);
//                bdInent.putExtra("TYPE","WANGQI");
                startActivity(bdInent);
            }
        });
        fqdListView.setOnRefreshListener(this);
        fqdListView.setOnLoadDataListener(this);
        fqdListView.doRefresh();


        cydListView.init(mContext, true);
        cydAdapter = new YueFanAdapter(mContext);
        cydListView.setAdapter(cydAdapter, true);
        cydListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (cydListView.getHeaderViewsCount() > 0) {
                    position--;
                }
                if (position < 0) return;
                HuoDongInfo fdinfo = (HuoDongInfo) cydAdapter.getItem(position);
                String activeid = fdinfo.getActivityId();
                String icon = fdinfo.getUserLogo();
                Intent bdInent = new Intent(mContext, YuFanDetailActivity.class);
                bdInent.putExtra("ACTIVEID", activeid);
                bdInent.putExtra("LOGO", icon);
                bdInent.putExtra("TYPE", "WANGQI");
                startActivity(bdInent);
            }
        });
        cydListView.setOnRefreshListener(this);
        cydListView.setOnLoadDataListener(this);
        cydListView.doRefresh();
        emptyLayout = (RelativeLayout) findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext, emptyLayout);

        getFaQiDeList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (fqdListView.getVisibility() == View.VISIBLE) {
            currentPage_fqd = 1;
            getFaQiDeList();

        } else if (cydListView.getVisibility() == View.VISIBLE) {
            currentPage_cyd = 1;
            getCanYuDeList();

        }
    }

    private void getFaQiDeList() {
        if (fqdTaskId != -1) {
            return;
        }
        if (currentPage_fqd == 0) {
            fqdListView.setNoNextPage();
            return;
        }

        MyHuoDongListTask task = new MyHuoDongListTask("1", size, currentPage_fqd);
        task.addListener(huodongListenter);
        fqdTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void getCanYuDeList() {
        if (cydTaskId != -1) {
            return;
        }
        if (currentPage_cyd == 0) {
            cydListView.setNoNextPage();
            return;
        }

        MyHuoDongListTask task = new MyHuoDongListTask("2", size, currentPage_cyd);
        task.addListener(huodongListenter);
        cydTaskId = getEatShowApp().getTaskManager().addTask(task);
    }


    private TaskEventListener huodongListenter = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == fqdTaskId) {
                fqdTaskId = -1;

                if (currentPage_fqd == 1) {
                    fqdAdapter.clearData();
                    fqdListView.onRefreshComplete();
                    fqdListView.enableLoadMore();
                } else {
                    fqdListView.onLoadDataComplete(true);
                }
                List<HuoDongInfo> fansList = new ArrayList<HuoDongInfo>();
                if (data != null) {
                    fansList = (List<HuoDongInfo>) data;
                }

                if (fansList != null && fansList.size() > 0) {
                    fqdAdapter.updateData(fansList);
                }
                // mAreaId="";
                currentPage_fqd = result;

                if (fqdAdapter != null && fqdAdapter.getCount() == 0) {
                    noDataUtil.ListViewEmpty(fqdListView, null, null, -1);
                } else {
                    noDataUtil.ListViewNoEmpty(fqdListView);
                }


            } else if (taskId == cydTaskId) {
                cydTaskId = -1;
                // sAdapter.clearData();
                if (currentPage_cyd == 1) {
                    cydAdapter.clearData();
                    cydListView.onRefreshComplete();
                    cydListView.enableLoadMore();
                } else {
                    cydListView.onLoadDataComplete(true);
                }
                List<HuoDongInfo> fansList = new ArrayList<HuoDongInfo>();
                if (data != null) {
                    fansList = (List<HuoDongInfo>) data;
                }

                if (fansList != null && fansList.size() > 0) {
                    cydAdapter.updateData(fansList);
                }
                //mAreaId="";
                currentPage_cyd = result;

                if (cydAdapter != null && cydAdapter.getCount() == 0) {
                    noDataUtil.ListViewEmpty(cydListView, null, null, -1);
                } else {
                    noDataUtil.ListViewNoEmpty(cydListView);
                }


            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == fqdTaskId) {
                fqdTaskId = -1;
                if (currentPage_fqd == 1) {
                    fqdListView.onRefreshComplete();
                } else {
                    fqdListView.onLoadDataComplete(false);
                }
            } else if (taskId == cydTaskId) {
                cydTaskId = -1;
                if (currentPage_cyd == 1) {
                    cydListView.onRefreshComplete();
                } else {
                    cydListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.createhd_btn:
                Intent cInent = new Intent(mContext, ChuangJianHuoDongActivity.class);
                startActivity(cInent);
                break;
            case R.id.faqide_btn:
                mFaQiDe.setChecked(true);
                mCanYuDe.setChecked(false);
                cydListView.setVisibility(View.GONE);
                fqdListView.setVisibility(View.VISIBLE);
                noDataUtil.ListViewNoEmpty(fqdListView);

                if (fqdAdapter != null && fqdAdapter.getCount() == 0) {
                    fqdAdapter.clearData();
                    currentPage_fqd = 1;
                    getFaQiDeList();
                }
                break;
            case R.id.canyude_btn:
                mCanYuDe.setChecked(true);
                mFaQiDe.setChecked(false);
                cydListView.setVisibility(View.VISIBLE);
                fqdListView.setVisibility(View.GONE);
                noDataUtil.ListViewNoEmpty(cydListView);

                if (cydAdapter != null && cydAdapter.getCount() == 0) {
                    cydAdapter.clearData();
                    currentPage_cyd = 1;
                    getCanYuDeList();
                }
                break;

        }
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        if (fqdListView.getVisibility() == View.VISIBLE) {
            getFaQiDeList();

        } else if (cydListView.getVisibility() == View.VISIBLE) {
            getCanYuDeList();

        }
    }

    @Override
    public void onRefresh() {
        if (fqdListView.getVisibility() == View.VISIBLE) {
            currentPage_fqd = 1;
            getFaQiDeList();

        } else if (cydListView.getVisibility() == View.VISIBLE) {
            currentPage_cyd = 1;
            getCanYuDeList();

        }
    }
}
